﻿/*
 *  BlackJack Simulator
 *  Feb 3rd 2013
 * 
 *  Game Screen
 * 
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BlackjackSimulator
{
    public partial class frmGameScreen : Form
    {
        public int playerCount = 1;
        public int wage = 0;
        public int deckCount = 1;
        public frmBlackJackMain main;
        public Game game;
        public frmGameScreen(int _playerCount, int _wage, int _deckCount)
        {
            InitializeComponent();
            this.playerCount = _playerCount;
            this.wage = _wage;
            this.deckCount = _deckCount;
            
            this.lblDealerCard.Text = "";
            this.lblPlayer1Card.Text = "";
            this.lblPlayer2Card.Text = "";
            this.lblPlayer3Card.Text = "";
            this.lblPlayer4Card.Text = "";
            this.lblPlayer5Card.Text = "";
            this.lblPlayer6Card.Text = "";
            this.lblChipCountSeat1.Text = "";
            this.lblChipCountSeat2.Text = "";
            this.lblChipCountSeat3.Text = "";
            this.lblChipCountSeat4.Text = "";
            this.lblChipCountSeat5.Text = "";
            this.lblChipCountSeat6.Text = "";
        }

        private void frmGameScreen_Load(object sender, EventArgs e)
        {
            btnSplit.Enabled = false;
            btnBet.Enabled = false;
            btnDoubleDown.Enabled = false;
            btnStay.Enabled = false;
            btnHit.Enabled = false;
            btnSurrender.Enabled = false;
            game = new Game(wage, deckCount, playerCount);
        }

        private void lblSeat1_Click(object sender, EventArgs e)
        {
            //lblSeat1.Text = "Me ";
            //playerCount++;
        }

        #region lblSeat
        private void lblSeat2_Click(object sender, EventArgs e)
        {
            //lblSeat2.Text = "Player " + playerCount;
            //playerCount++;
        }

        private void lblSeat3_Click(object sender, EventArgs e)
        {
            //lblSeat3.Text = "Player " + playerCount;
            //playerCount++;
        }

        private void lblSeat4_Click(object sender, EventArgs e)
        {
            //lblSeat4.Text = "Player " + playerCount;
            //playerCount++;
        }

        private void lblSeat5_Click(object sender, EventArgs e)
        {
            //lblSeat5.Text = "Player " + playerCount;
            //playerCount++;
        }

        private void lblSeat6_Click(object sender, EventArgs e)
        {
            //lblSeat6.Text = "Player " + playerCount;
            //playerCount++;
        }
        #endregion

        private void btnExit_Click(object sender, EventArgs e)
        {
            //close the form
            this.Close();
        }

        protected override void OnClosed(EventArgs e)
        {
            main.Show();
            base.OnClosed(e);
        }

        private void initWhenEndGame()
        {
            btnBet.Enabled = true;
            btnHit.Enabled = false;
            //btnStartGame.Enabled = true;
            btnExit.Enabled = true;
            btnStay.Enabled = false;
            btnDoubleDown.Enabled = false;
            btnSurrender.Enabled = false;
            string mess = "";
            mess += "Result Table:\n";
            mess += "Dealer: " + game.CardDealer.EndGameDisplay() + "\n";
            mess += "Me: " + game.Me.Display() + " is ";
            if (game.Me.IsWin)
                mess += "Winner";
            else
                mess += "Loser";
            mess += " - " + game.Me.Money.ToString() + " chips\n";
            for (int i = 0; i < game.OtherPlayer.Count; i++)
            {
                mess += "Player " + (i + 2).ToString() + ": " + game.OtherPlayer[i].EndGameDisplay() + " is ";
                if (game.OtherPlayer[i].IsWin)
                    mess += "Winner";
                else
                    mess += "Loser"; 
                mess += " - " + game.OtherPlayer[i].Money.ToString() + " chips\n";
            }
            MessageBox.Show(mess);
        }

        private void btnHit_Click(object sender, EventArgs e)
        {
            if (game.Me.Hand1.IsSplit == false)
            {
                if (game.Me.Hand1.IsBust == false && game.Me.Hand1.IsStand == false && game.Me.Hand1.IsDoubleDown == false)
                {                
                    game.Me.Hand1.Add(game.DealOneCard());
                    lblPlayer1Card.Text = game.Me.Display();
                    game.Me.Hand1.CheckBust();
                    if (game.Me.Hand1.IsBust == true)
                    {
                        game.Me.Money -= game.MoneyBet;
                        MessageBox.Show("Bust!");
                        game.AutomaticPlay();
                        initWhenEndGame();
                    }                    
                }
            }
            else
            {
                if (game.Me.Hand1.IsDoubleDown == true || game.Me.Hand1.IsBust == true || game.Me.Hand1.IsStand == true)
                {
                    game.Me.Hand2.Add(game.DealOneCard());
                    lblPlayer1Split.Text = game.Me.DisplaySplit();
                    game.Me.Hand2.CheckBust();
                    if (game.Me.Hand2.IsBust == true)
                    {
                        game.Me.Money -= game.MoneyBet;
                        MessageBox.Show("Bust!");
                        game.AutomaticPlay();
                        initWhenEndGame();
                    }
                }
                else
                {
                    game.Me.Hand1.Add(game.DealOneCard());
                    lblPlayer1Card.Text = game.Me.Display();
                    game.Me.Hand1.CheckBust();
                    if (game.Me.Hand1.IsBust == true)
                    {
                        game.Me.Money -= game.MoneyBet;
                        MessageBox.Show("Bust!");
                        game.Me.Hand2.Add(game.DealOneCard());
                        lblPlayer1Split.Text = game.Me.DisplaySplit();
                    }
                    
                }
            }

            
        }

        private void btnBet_Click(object sender, EventArgs e)
        {
            if (game.Me.Money >= 5)
            {
                SetBet setbetForm = new SetBet(this, game.Me.Money);
                setbetForm.ShowDialog();
                btnBet.Enabled = false;
                startGame();
            }
            else
            {
                MessageBox.Show("Out of money!");
            }
        }

        private void btnStay_Click(object sender, EventArgs e)
        {
            if (game.Me.Hand1.IsSplit == false)
            {
                game.Me.Hand1.IsStand = true;
                btnHit.Enabled = false;
                btnDoubleDown.Enabled = false;
                btnSplit.Enabled = false;
                btnSurrender.Enabled = false;
                game.AutomaticPlay();
                initWhenEndGame();
            }
            else
            {
                if (game.Me.Hand1.IsStand == true)
                {
                    game.Me.Hand2.IsStand = true;
                    btnHit.Enabled = false;
                    btnDoubleDown.Enabled = false;
                    btnSplit.Enabled = false;
                    btnSurrender.Enabled = false;
                    game.AutomaticPlay();
                    initWhenEndGame();
                }
                else
                {
                    game.Me.Hand1.IsStand = true;
                    game.Me.Hand2.Add(game.DealOneCard());
                    lblPlayer1Split.Text = game.Me.DisplaySplit();
                }
            }
        }

        private void btnDoubleDown_Click(object sender, EventArgs e)
        {
            if(game.Me.Hand1.IsDoubleDown == false)
            {
                game.Me.Hand1.Add(game.DealOneCard());
                lblPlayer1Card.Text = game.Me.Display();
                game.Me.Hand1.CheckBust();
                if (game.Me.Hand1.IsBust == true)
                {
                    MessageBox.Show("Bust!");
                    game.Me.Money -= game.MoneyBet * 2;
                    game.AutomaticPlay();
                    initWhenEndGame();
                }
                game.Me.Hand1.IsDoubleDown = true;
                if (game.Me.Hand1.IsSplit == true)
                {
                    game.Me.Hand2.Add(game.DealOneCard());
                    lblPlayer1Split.Text = game.Me.DisplaySplit();
                }
                else
                    btnDoubleDown.Enabled = false;
                return;
            }
            if (game.Me.Hand1.IsSplit == true)
            {
                if (game.Me.Hand1.IsDoubleDown == true)
                {
                    game.Me.Hand2.Add(game.DealOneCard());
                    lblPlayer1Split.Text = game.Me.DisplaySplit();
                    game.Me.Hand2.CheckBust();
                    if (game.Me.Hand2.IsBust == true)
                    {
                        MessageBox.Show("Bust!");
                        game.Me.Money -= game.MoneyBet * 2;
                        game.AutomaticPlay();
                        initWhenEndGame();
                    }
                    game.Me.Hand2.IsDoubleDown = true;
                    btnDoubleDown.Enabled = false;
                }
            }                        
        }

        private void btnSplit_Click(object sender, EventArgs e)
        {
            game.Me.SplitAPair();
            game.Me.Hand1.Add(game.DealOneCard());
            lblPlayer1Card.Text =   game.Me.Display();
            lblPlayer1Split.Visible = true;
            lblPlayer1Split.Text = game.Me.DisplaySplit();
            game.Me.Hand1.IsSplit = true;
        }

        private void btnSurrender_Click(object sender, EventArgs e)
        {
            if (game.Me.Hand1.IsSplit == false)
            {
                game.Me.Hand1.IsSurrender = true;
                game.Me.Money -= game.MoneyBet / 2;
                btnHit.Enabled = false;
                btnSplit.Enabled = false;
                btnDoubleDown.Enabled = false;
                btnStay.Enabled = false;
                game.AutomaticPlay();
                initWhenEndGame();
            }
            else
            {
                if (game.Me.Hand1.IsSurrender == false)
                {
                    game.Me.Hand1.IsSurrender = true;
                    game.Me.Money -= game.MoneyBet / 2;
                }
                else
                {
                    game.Me.Hand2.IsSurrender = true;
                    game.Me.Money -= game.MoneyBet / 2;
                    btnHit.Enabled = false;
                    btnSplit.Enabled = false;
                    btnDoubleDown.Enabled = false;
                    btnStay.Enabled = false;
                    game.AutomaticPlay();
                    initWhenEndGame();
                }
            }
        }

        private void btnStartGame_Click(object sender, EventArgs e)
        {
            startGame();
            btnStartGame.Enabled = false;
        }

        private void startGame()
        {            
            game.Start(wage);

            btnSurrender.Enabled = true;
            btnHit.Enabled = true;
            btnDoubleDown.Enabled = true;
            btnStay.Enabled = true;

            DisplayStartGame();
            if (game.Me.Hand1.isBlackJack() == true)
            {
                MessageBox.Show("Black Jack!");
            }
            if (game.Me.checkSplit() == true)
                btnSplit.Enabled = true;
            else
                btnSplit.Enabled = false;

            lblPlayer1Split.Visible = false;
            btnDoubleDown.Enabled = true;
        }

        public void DisplayStartGame()
        {
            lblSeat1.Text = "Me";
            lblPlayer1Card.Text = game.Me.Display();
            lblChipCountSeat1.Text = game.Me.Money.ToString();

            lblDealerCard.Text = game.CardDealer.Display();
            switch(playerCount)
            {
                case 1:
                    lblSeat2.Text = "";
                    lblSeat3.Text = "";
                    lblSeat4.Text = "";
                    lblSeat5.Text = "";
                    lblSeat6.Text = "";            
                    break;
                case 2:
                    lblSeat2.Text = "Player 2";
                    lblSeat3.Text = "";
                    lblSeat4.Text = "";
                    lblSeat5.Text = "";
                    lblSeat6.Text = "";
                    lblPlayer2Card.Text = game.OtherPlayer[0].Display();
                    lblChipCountSeat2.Text = game.OtherPlayer[0].Money.ToString();
                    break;
                case 3:
                    lblSeat2.Text = "Player 2";
                    lblSeat3.Text = "Player 3";
                    lblSeat4.Text = "";
                    lblSeat5.Text = "";
                    lblSeat6.Text = "";
                    lblPlayer2Card.Text = game.OtherPlayer[0].Display();
                    lblPlayer3Card.Text = game.OtherPlayer[1].Display();
                    lblChipCountSeat2.Text = game.OtherPlayer[0].Money.ToString();
                    lblChipCountSeat3.Text = game.OtherPlayer[1].Money.ToString();
                    break;
                case 4:
                    lblSeat2.Text = "Player 2";
                    lblSeat3.Text = "Player 3";
                    lblSeat4.Text = "Player 4";
                    lblSeat5.Text = "";
                    lblSeat6.Text = "";
                    lblPlayer2Card.Text = game.OtherPlayer[0].Display();
                    lblPlayer3Card.Text = game.OtherPlayer[1].Display();
                    lblPlayer4Card.Text = game.OtherPlayer[2].Display();
                    lblChipCountSeat2.Text = game.OtherPlayer[0].Money.ToString();
                    lblChipCountSeat3.Text = game.OtherPlayer[1].Money.ToString();
                    lblChipCountSeat4.Text = game.OtherPlayer[2].Money.ToString();
                    break;
                case 5:
                    lblSeat2.Text = "Player 2";
                    lblSeat3.Text = "Player 3";
                    lblSeat4.Text = "Player 4";
                    lblSeat5.Text = "Player 5";
                    lblSeat6.Text = "";
                    lblPlayer2Card.Text = game.OtherPlayer[0].Display();
                    lblPlayer3Card.Text = game.OtherPlayer[1].Display();
                    lblPlayer4Card.Text = game.OtherPlayer[2].Display();
                    lblPlayer5Card.Text = game.OtherPlayer[3].Display();
                    lblChipCountSeat2.Text = game.OtherPlayer[0].Money.ToString();
                    lblChipCountSeat3.Text = game.OtherPlayer[1].Money.ToString();
                    lblChipCountSeat4.Text = game.OtherPlayer[2].Money.ToString();
                    lblChipCountSeat5.Text = game.OtherPlayer[3].Money.ToString();
                    break;
                case 6:
                    lblSeat2.Text = "Player 2";
                    lblSeat3.Text = "Player 3";
                    lblSeat4.Text = "Player 4";
                    lblSeat5.Text = "Player 5";
                    lblSeat6.Text = "Player 6";
                    lblPlayer2Card.Text = game.OtherPlayer[0].Display();
                    lblPlayer3Card.Text = game.OtherPlayer[1].Display();
                    lblPlayer4Card.Text = game.OtherPlayer[2].Display();
                    lblPlayer5Card.Text = game.OtherPlayer[3].Display();
                    lblPlayer6Card.Text = game.OtherPlayer[4].Display();
                    lblChipCountSeat2.Text = game.OtherPlayer[0].Money.ToString();
                    lblChipCountSeat3.Text = game.OtherPlayer[1].Money.ToString();
                    lblChipCountSeat4.Text = game.OtherPlayer[2].Money.ToString();
                    lblChipCountSeat5.Text = game.OtherPlayer[3].Money.ToString();
                    lblChipCountSeat6.Text = game.OtherPlayer[4].Money.ToString();
                    break;
            }
        }

        public void DisplayEndGame()
        {
            switch (playerCount)
            {
                case 1:
                    lblPlayer1Card.Text = game.Me.Display();
                    break;
                case 2:
                    lblPlayer1Card.Text = game.Me.Display();
                    lblPlayer2Card.Text = game.OtherPlayer[0].EndGameDisplay();
                    break;
                case 3:
                    lblPlayer1Card.Text = game.Me.Display();
                    lblPlayer2Card.Text = game.OtherPlayer[0].EndGameDisplay();
                    lblPlayer3Card.Text = game.OtherPlayer[1].EndGameDisplay();
                    break;
                case 4:
                    lblPlayer1Card.Text = game.Me.Display();
                    lblPlayer2Card.Text = game.OtherPlayer[0].EndGameDisplay();
                    lblPlayer3Card.Text = game.OtherPlayer[1].EndGameDisplay();
                    lblPlayer4Card.Text = game.OtherPlayer[2].EndGameDisplay();
                    break;
                case 5:
                    lblPlayer1Card.Text = game.Me.Display();
                    lblPlayer2Card.Text = game.OtherPlayer[0].EndGameDisplay();
                    lblPlayer3Card.Text = game.OtherPlayer[1].EndGameDisplay();
                    lblPlayer4Card.Text = game.OtherPlayer[2].EndGameDisplay();
                    lblPlayer5Card.Text = game.OtherPlayer[3].EndGameDisplay();
                    break;
                case 6:
                    lblPlayer1Card.Text = game.Me.Display();
                    lblPlayer2Card.Text = game.OtherPlayer[0].EndGameDisplay();
                    lblPlayer3Card.Text = game.OtherPlayer[1].EndGameDisplay();
                    lblPlayer4Card.Text = game.OtherPlayer[2].EndGameDisplay();
                    lblPlayer5Card.Text = game.OtherPlayer[3].EndGameDisplay();
                    lblPlayer6Card.Text = game.OtherPlayer[4].EndGameDisplay();

                    break;
            }
        }

        private void lblPlayer1Card_Click(object sender, EventArgs e)
        {

        }

        private void lblPlayer1Split_Click(object sender, EventArgs e)
        {

        }
    }
}
